package com.cgtong.android.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.ImageView;

import com.cgtong.android.R;
import com.cgtong.android.util.StatusBarUtil;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.dyhdyh.widget.loading.factory.MaterialDialogFactory;

import butterknife.ButterKnife;

/**
 * Created by zhuqingchun
 * 2017/9/30 14:50.
 */

public abstract class BaseActivity extends AppCompatActivity {


    public abstract int getLayoutId();
    public abstract void initData();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        initStatusBar();
        initData();
    }

    /**
     * 子类可以重写改变状态栏颜色
     */
    protected int setStatusBarColor() {
        return R.color.colorPrimary;
    }

    /**
     * 子类可以重写决定是否使用透明状态栏
     */
    protected boolean setTranslucentStatusBar() {
        return false;
    }

    /**
     * 设置状态栏颜色
     */
    protected void initStatusBar() {
        if (setTranslucentStatusBar()) {
            // 设置状态栏全透明
            StatusBarUtil.transparencyBar(this);
            return;
        }
        StatusBarUtil.setStatusBarColor(this, setStatusBarColor());
    }

    /**
     * 显示加载框
     */
    public void doShowLoading(Context context, boolean cancelable) {
        this.doShowLoading(context, null, cancelable);
    }

    public void doShowLoading(Context context, String msg, boolean cancelable) {
        LoadingDialog loadingDialog = LoadingDialog.make(context, new MaterialDialogFactory());
        if (!TextUtils.isEmpty(msg)) {
            loadingDialog.setMessage(msg);//提示消息
        } else {
            loadingDialog.setMessage(getString(R.string.request_net))//提示消息
                    .setCancelable(cancelable);
        }
        loadingDialog.create().show();
    }

    public void doHideLoading() {
        LoadingDialog.cancel();
    }

    /**
     * 显示用户头像（本地）
     */
    public void setUserAvatar(ImageView imageView, int avatarId) {
        TypedArray avatars = getResources().obtainTypedArray(R.array.avatars);
        imageView.setImageResource(avatars.getResourceId(avatarId - 1, 1));
    }
}
